package com.dns.server.secondary;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;

import com.dns.message.DNS_Message;
import com.dns.util.Constants;


public class DNSClient {

	private static String serverIP;
	private static int serverPort;
	private static int clientPort;
	private static String clientIP = "127.0.0.1";
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		int index=0;
		String requestType = args[index];
		++index;
		clientPort = Integer.parseInt(args[index]);
		++index;
		serverIP = args[index];
		++index;		
		serverPort = Integer.parseInt(args[index]);	
		
		try
		{
		if(requestType.equals("DNS"));
			dnsRequest(args);
		if(requestType.equals("REVERSE_DNS"))
			reverseDNS(args);
		if(requestType.equals("UPDATE"))
			update(args);
		}catch(Exception exception)
		{ 
			System.err.println("Exception @DNSClient: "+exception.getMessage());
		}
	}
	
	private static void dnsRequest(String args[]) throws SocketException
	{
		String domain = args[4];
		DNS_Message msg = new DNS_Message();
		
		msg.setClientAddress(clientIP);
		msg.setClientPort(clientPort);
		msg.setDomainName(domain);		
		msg.setMsgType(Constants.MSG_INQUIRY);
		msg.setSource(Constants.PRIMARY);
		
		sendMessage(msg);
	}

	private static void reverseDNS(String args[]) throws SocketException
	{
		String ip = args[4];
		DNS_Message msg = new DNS_Message();
		
		msg.setClientAddress(clientIP);
		msg.setClientPort(clientPort);
		msg.setIp_address(ip);		
		msg.setMsgType(Constants.MSG_INQUIRY);
		msg.setSource(Constants.PRIMARY);
		
		sendMessage(msg);		
	}

	private static void update(String args[]) throws SocketException
	{
		String domain = args[4];
		String ip = args[5];
		DNS_Message msg = new DNS_Message();
		
		msg.setClientAddress(clientIP);
		msg.setClientPort(clientPort);
		msg.setIp_address(ip);
		msg.setDomainName(domain);
		msg.setMsgType(Constants.MSG_UPDATE);
		msg.setSource(Constants.CLIENT);
		
		sendMessage(msg);	
	}
	
	private static void sendMessage(DNS_Message msg) throws SocketException
	{
		DatagramPacket sendPacket;
		byte []buff = new byte[Constants.DATA_BUFFER_SIZE];
		DatagramSocket clientSocket = new DatagramSocket(clientPort);
		
		ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
		ObjectOutputStream objectOut;
		try {
			objectOut = new ObjectOutputStream(byteOut);
			objectOut.writeObject(msg);
			buff =byteOut.toByteArray();
			sendPacket = new DatagramPacket(buff, buff.length, InetAddress.getByName(serverIP), serverPort);
			clientSocket.send(sendPacket);
		} catch (IOException e) {
			System.out.println(" : Exception while creating the serializing or writing the DNS_Message object");
			e.printStackTrace();
		}
		finally{
			byteOut=null;
			objectOut=null;
			buff=null;
			clientSocket.close();
		}		
	}
}
